<?php
namespace Home\Controller;
use Think\Controller;

/**
 * Class RoomController
 * @package Home\Controller
 */
class MemberController extends Controller{
	public function _initialize(){
		$this -> user = D("User");
		$where['id'] = session("userid");
		$res = $this -> user -> where($where) -> select();
		if ( $res ) {
			$this -> assign("userinfo",$res[0]);
		}
	}
	
	public function login(){
		$this->display();
	}

	public function self(){
		$this -> display();
	}

	public function loginout(){
		session("userid",null);
		$this -> display("login");
	}

	public function yzinfo(){
		$data['id'] = session('userid');
		if ($_POST['name']) {
			$data['name'] = $_POST['name'];
		}
		if ($_POST['cardid']) {
			$data['cardid'] = $_POST['cardid'];
		}
		if ($_POST['zhiw']) {
			$data['zhiw'] = $_POST['zhiw'];
		}
		if ($_POST['hy']) {
			$data['hy'] = $_POST['hy'];
		}
		$res = $this -> user -> save($data);
		if ($res) {
			apiResponse("success","保存成功！");
		}else{
			apiResponse("error","保存失败！");
		}
	}


	public function yanInfo(){
		$data['name'] = $_POST['name'];
        $data['cardid'] = $_POST['cardid'];
        $data['tel'] = $_POST['account'];
        $data['code'] = $_POST['code'];
        if ( $data['name'] && $data['cardid'] && $data['tel'] && $data['code']) {
        	if ($data['code'] == session("code")) {
        		$data['id'] = session('userid');
        		$res = $this -> user -> save($data); 
    			apiResponse("success","验证成功！");
        	}else{
        		apiResponse("error","验证码错误！");
        	}
        }else{
        	apiResponse("error","信息填写不完整！");
        }
	}

	public function register(){
		$tel = $_POST['tel'];
		$code = $_POST['code'];
		if ($tel && $code) {
			if ($code == session("code")) {
				$where['tel'] = $tel;
				$user = $this -> user -> where($where) -> limit(1) -> select();
				$userid = session('userid');
				if ( empty($userid) ) {
					if (empty($user)) {
						$data = array(
							'tel' => $tel,
							'name' => $tel,
							'ctime' => time(),
							'status' => 0
							);
						$res = $this -> user -> add($data);
					}else{
						$res = $user[0]['id'];
					}
					session("userid",$res);
				}else{
					$data = array(
						'tel' => $tel,
						'id' => $userid
						);
					$res = $this -> user -> save($data);
				}
				session("tel",$tel);
				apiResponse("success","验证成功！");
			}else{
				apiResponse("error","验证码错误！");
			}
		}else{
			apiResponse("error","用户名或密码为空！");
		}
	}

	public function getCode(){
		$tel = $_POST['tel'];
		$code = mt_rand(2000,9999);
		if (empty($tel)) {
			apiResponse("error","手机号为空！");
		}
		$sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
		$smsConf = array(
		    'key'   => 'df3c17a2534919ce681b4c213cf68962', //您申请的APPKEY
		    'mobile'    => $tel, //接受短信的用户手机号码
		    'tpl_id'    => '34145', //您申请的短信模板ID，根据实际情况修改
		    'tpl_value' =>'#code#='.$code //您设置的模板变量，根据实际情况修改
		);
		$content = $this -> juhecurl($sendUrl,$smsConf,1); //请求发送短信
		 
		if($content){
		    $result = json_decode($content,true);
		    $error_code = $result['error_code'];
		    if($error_code == 0){
		        //状态为0，说明短信发送成功
		        //echo "短信发送成功,短信ID：".$result['result']['sid'];
		        session("code",$code);
		    	apiResponse("success","短信发送成功！");
		    }else{
		        //状态非0，说明失败
		        $msg = $result['reason'];
		        //echo "短信发送失败(".$error_code.")：".$msg;
		        apiResponse("error","短信发送失败！$msg");
		    }
		}else{
		    //返回内容异常，以下可根据业务逻辑自行修改
		    apiResponse("error","请求发送短信失败!");
		}
	}

	/**
     * 请求接口返回内容
     * @param  string $url [请求的URL地址]
     * @param  string $params [请求的参数]
     * @param  int $ipost [是否采用POST形式]
     * @return  string
     */
    public function juhecurl($url,$params=false,$ispost=0){
        $httpInfo = array();
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
        curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
        if( $ispost )
        {
            curl_setopt( $ch , CURLOPT_POST , true );
            curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
            curl_setopt( $ch , CURLOPT_URL , $url );
        }
        else
        {
            if($params){
                curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
            }else{
                curl_setopt( $ch , CURLOPT_URL , $url);
            }
        }
        $response = curl_exec( $ch );
        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
        $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
        curl_close( $ch );
        return $response;
    }

}
